{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "private_outputs": true,
      "provenance": [],
      "authorship_tag": "ABX9TyOqpl6j73bVPIF7YTpq/mQE",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/ktynski/Marketing_Automations_Notebooks_With_GPT/blob/main/Automatic_Question_Expander_ala_AnswerThePublic_(Public).ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "RmxlzPiwAakK"
      },
      "outputs": [],
      "source": [
        "!pip install openai"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "import openai\n",
        "import pandas as pd\n",
        "import concurrent.futures\n",
        "\n",
        "openai.api_key = \"your Openai api key\"\n",
        "\n",
        "\n",
        "\n",
        "def generate_text(prompt):\n",
        "    response = openai.Completion.create(\n",
        "        engine=\"text-davinci-003\",\n",
        "        prompt=prompt,\n",
        "        max_tokens=1500,\n",
        "        n=1,\n",
        "        stop=None,\n",
        "        temperature=0.6,\n",
        "    )\n",
        "    return response.choices[0].text\n",
        "\n",
        "def generate_subtopics(topic):\n",
        "    prompt = f\"Generate 40 common subtopics related to {topic}\"\n",
        "    subtopics = generate_text(prompt).strip().split(\"\\n\")\n",
        "    return subtopics\n",
        "\n",
        "def generate_subsubtopics(subtopic):\n",
        "    prompt = f\"Generate 20 common subsubtopics related to {subtopic}\"\n",
        "    subsubtopics = generate_text(prompt).strip().split(\"\\n\")\n",
        "    print(subsubtopics)\n",
        "    return subsubtopics\n",
        "\n",
        "def generate_questions(topic, subtopic, subsubtopic):\n",
        "    prompt = f\"\"\"You are an all knowing AI that specializes in search behavior, SEO, and search intent.\n",
        "    You are especially good at predicting questions related to topics.\n",
        "    Please Generate 30 of the most common questions related to {subsubtopic}\n",
        "    within the context of {subtopic} in the larger topic of {topic}\"\"\"\n",
        "    questions = generate_text(prompt).strip().split(\"\\n\")\n",
        "    return [question.split(\". \")[-1] for question in questions]\n",
        "\n",
        "def process_subsubtopic(topic, subtopic, subsubtopic):\n",
        "    questions = generate_questions(topic, subtopic, subsubtopic)\n",
        "    data = {\n",
        "        \"topic\": topic,\n",
        "        \"subtopic\": subtopic,\n",
        "        \"subsubtopic\": subsubtopic,\n",
        "    }\n",
        "    for i, question in enumerate(questions):\n",
        "        data[f\"Question {i + 1}\"] = question\n",
        "    return data\n",
        "\n",
        "def main(topic):\n",
        "    subtopics = generate_subtopics(topic)\n",
        "    data = []\n",
        "    with concurrent.futures.ThreadPoolExecutor() as executor:\n",
        "        for subtopic in subtopics:\n",
        "            subsubtopics = generate_subsubtopics(subtopic)\n",
        "            results = [executor.submit(process_subsubtopic, topic, subtopic, subsubtopic) for subsubtopic in subsubtopics]\n",
        "            for f in concurrent.futures.as_completed(results):\n",
        "                data.append(f.result())\n",
        "    df = pd.DataFrame(data)\n",
        "    return df\n",
        "\n",
        "if __name__ == \"__main__\":\n",
        "    topic = \"Home Security\"\n",
        "    df = main(topic)\n",
        "    print(df)\n"
      ],
      "metadata": {
        "id": "PyQcgc3Cq52v"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "df.to_csv(\"homesecurityquestions2.csv\")"
      ],
      "metadata": {
        "id": "6ZdAfRu8uDIY"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}